import request from '@/utils/request'

// 获取车队组织架构树
export function getFleetOrganizationTree() {
  return request({
    url: '/system/fleet/tree',
    method: 'get'
  })
}

// 获取组织架构统计概览
export function getOrganizationOverview() {
  return request({
    url: '/system/fleet/overview',
    method: 'get'
  })
}

// 获取区域列表及统计信息
export function getAreasWithStatistics() {
  return request({
    url: '/system/fleet/areas',
    method: 'get'
  })
}

// 获取车队列表及统计信息
export function getFleetsWithStatistics(areaId) {
  return request({
    url: '/system/fleet/fleets',
    method: 'get',
    params: { areaId }
  })
}

// 获取车队详细统计信息
export function getFleetStatistics(fleetId) {
  return request({
    url: `/system/fleet/fleets/${fleetId}/statistics`,
    method: 'get'
  })
}

// 获取车队下的车辆列表
export function getVehiclesByFleetId(fleetId) {
  return request({
    url: `/system/fleet/fleets/${fleetId}/vehicles`,
    method: 'get'
  })
}

// 搜索车辆
export function searchVehicles(keyword, areaId, fleetId, status) {
  return request({
    url: '/system/fleet/vehicles/search',
    method: 'get',
    params: {
      keyword,
      areaId,
      fleetId,
      status
    }
  })
}

// 获取低电量车辆列表
export function getLowBatteryVehicles(threshold = 20.0) {
  return request({
    url: '/system/fleet/vehicles/low-battery',
    method: 'get',
    params: { threshold }
  })
}

// 车辆调配到指定车队
export function transferVehicleToFleet(vehicleId, targetFleetId) {
  return request({
    url: `/system/fleet/vehicles/${vehicleId}/transfer`,
    method: 'post',
    params: { targetFleetId }
  })
}

// 批量车辆调配
export function batchTransferVehicles(vehicleIds, targetFleetId) {
  return request({
    url: '/system/fleet/vehicles/batch-transfer',
    method: 'post',
    data: vehicleIds,
    params: { targetFleetId }
  })
}

// 获取车队组织架构数据（用于下拉选择）
export function getFleetOptions() {
  return request({
    url: '/system/fleet/options',
    method: 'get'
  })
}

// 车辆管理相关接口

// 获取车辆列表（分页）
export function getVehicleList(params) {
  return request({
    url: '/system/vehicle/list',
    method: 'get',
    params
  })
}

// 获取车辆详情
export function getVehicleDetail(vehicleId) {
  return request({
    url: `/system/vehicle/${vehicleId}`,
    method: 'get'
  })
}

// 新增车辆
export function addVehicle(data) {
  return request({
    url: '/system/vehicle',
    method: 'post',
    data
  })
}

// 修改车辆
export function updateVehicle(data) {
  return request({
    url: '/system/vehicle',
    method: 'put',
    data
  })
}

// 删除车辆
export function deleteVehicle(vehicleId) {
  return request({
    url: `/system/vehicle/${vehicleId}`,
    method: 'delete'
  })
}

// 批量删除车辆
export function batchDeleteVehicles(vehicleIds) {
  return request({
    url: '/system/vehicle/batch',
    method: 'delete',
    data: vehicleIds
  })
}

// 更新车辆状态
export function updateVehicleStatus(vehicleId, status) {
  return request({
    url: `/system/vehicle/${vehicleId}/status`,
    method: 'put',
    data: { status }
  })
}

// 车辆绑定司机
export function bindVehicleDriver(vehicleId, driverId) {
  return request({
    url: `/system/vehicle/${vehicleId}/bind-driver`,
    method: 'post',
    data: { driverId }
  })
}

// 车辆解绑司机
export function unbindVehicleDriver(vehicleId) {
  return request({
    url: `/system/vehicle/${vehicleId}/unbind-driver`,
    method: 'post'
  })
}

// 获取车辆状态统计
export function getVehicleStatusStatistics() {
  return request({
    url: '/system/vehicle/statistics/status',
    method: 'get'
  })
}

// 获取车辆电量统计
export function getVehicleBatteryStatistics() {
  return request({
    url: '/system/vehicle/statistics/battery',
    method: 'get'
  })
}

// 导出车辆数据
export function exportVehicleData(params) {
  return request({
    url: '/system/vehicle/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
}

// 车队管理相关接口

// 获取车队列表（分页）
export function getFleetList(params) {
  return request({
    url: '/system/fleet/list',
    method: 'get',
    params
  })
}

// 新增车队
export function addFleet(data) {
  return request({
    url: '/system/fleet',
    method: 'post',
    data
  })
}

// 修改车队
export function updateFleet(data) {
  return request({
    url: '/system/fleet',
    method: 'put',
    data
  })
}

// 删除车队
export function deleteFleet(fleetId) {
  return request({
    url: `/system/fleet/${fleetId}`,
    method: 'delete'
  })
}

// 区域管理相关接口

// 获取区域列表（分页）
export function getAreaList(params) {
  return request({
    url: '/system/area/list',
    method: 'get',
    params
  })
}

// 新增区域
export function addArea(data) {
  return request({
    url: '/system/area',
    method: 'post',
    data
  })
}

// 修改区域
export function updateArea(data) {
  return request({
    url: '/system/area',
    method: 'put',
    data
  })
}

// 删除区域
export function deleteArea(areaId) {
  return request({
    url: `/system/area/${areaId}`,
    method: 'delete'
  })
}